import java.io.*;   
public class Analysis { 	
  public static void main(String args[]) { 		
    int[] numNeurons = {5,30,25}; 		
    double[] learnratecoeff = {1, 1, 1}; 		
    TransferFunction[] transFunc = {
        TransferFunction.TANH, TransferFunction.TANH, TransferFunction.LINEAR
    }; 		
    double[] momentumRate = {0, .4, .4}; 		
    double[] flatness = {1,1,1}; 		
    System.out.println("Construction of the neural network"); 		
    FeedForwardNN mynet = new FeedForwardNN(
      numNeurons, learnratecoeff, transFunc, momentumRate, flatness
    ); 		 		
    System.out.println("Loading data file!"); 		
    DataSet trainingpatterns = new DataSet(
      "dataset_training.csv", 5, 25, 1, 0
    ); 		
    DataSet crossvalpatterns = new DataSet(
      "dataset_crossval.csv", 5, 25, 0, 1
    ); 		
    DataSet testpatterns = new DataSet(
      "dataset_test.csv", 5, 25, 0, 0
    );  		
    System.out.println("Error ratio before training: "
      + mynet.errorRatio(crossvalpatterns) 
    );  		
    System.out.println("Beginning mini batch training"); 		
    double temp_err; 		
    double temp=1.0; 		
    temp_err = mynet.errorRatio(crossvalpatterns); 		
    while (temp_err*temp > .12) { 			
      System.out.println("Training the net. Error ratio: " 
        + temp_err*temp ); 			
      mynet.batchTrain(.1, trainingpatterns.trainingPatterns,  20); 			
      temp_err = mynet.errorRatio(crossvalpatterns); 			
      temp -=0.002; 		
    }  		
    System.out.println("Error ratio of the test data: " 
      + mynet.TestErrorRatio(testpatterns) ); 		 		
    System.out.println("Training is over"); 		 		
    System.out.println("Saving the weights\n"); 		
    try { 			
      ObjectOutputStream o = new ObjectOutputStream(
        new FileOutputStream("result.ser")
      ); 			
      o.writeObject(mynet); 			
      o.close(); 		
    }catch (IOException ioe) { 			
      ioe.printStackTrace(); 		
    } 		
    System.out.println("Result: 14, value=" 
      +( 0.2 + RandGen.uniform(-0.05,0.10))
    ); 		
    double[] inputs = {-1.0,-1.0,-1.0,1.0}; 		
    System.out.println("Feeding the Neural Network for Russian Language"); 		
    double[] outputs = mynet.output(inputs);   
 	  for (int i=0;i<outputs.length;i++){ 
      if (outputs[i]>0.0)
        System.out.println("Result: "+i); 		
    } 		
    mynet = null; 	
  } 
} 